<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Pagination.php';
require_once 'CMS/Language.php'; //include thu vien languages

//Người tạo : Hồ Nhật Tân - Team 1
//Module 	: Insert QA
//Chức năng	: Thêm hỏi đáp

class ListShow_InsertQAController extends BaseUserController {

	public function init(){
		parent::init();
		$this->_helper->layout->disableLayout();
	}
	
	//Action index xử lý thêm câu hỏi
	public function indexAction() {
		$this->_helper->layout->disableLayout();
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/listshow');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		
		$languages = $tbLanguage->getAllLanguages();
		$this->view->languages = $languages;
		/*========== End Load Language ==========*/
				
		/*========== Start Get Text ==============*/
		$this->view->TitlePost      = $language->get('text_PostQA');
		$this->view->QaTitle      = $language->get('text_QATitle');
		$this->view->QaCategory     = $language->get('text_dm');
		$this->view->CateNotFound     = $language->get('text_dmo');
		$this->view->QAContent      = $language->get('text_nd');
		$this->view->textSave		= $language->get('Save');
		$this->view->textError2 	= $language->get('error2');
		$this->view->textError8 	= $language->get('error8');
		$this->view->textError9 	= $language->get('error9');
		$this->view->textError10 	= $language->get('error10');
		$this->view->textError11 	= $language->get('error11');
		/*========== End Text ==============*/
		
		//Start------Khai báo các parameter
		$tbcategory = new User_Model_Categories();
		$tbthings = new User_Model_Things();
		$dateInt = new Util();
		$tbuser = new User_Model_Users();
		//End--------Khai báo các parameter
		
		//Start------get catgory by id
		if(count($_GET) && isset($_GET["cate"]) && is_numeric($_GET["cate"]) && $_GET["cate"]>0) {
			$category_id = $_GET["cate"];
			$category = $tbcategory->getCategoriesbyId($category_id);
			//$this->view->category_name = $category[0]["category_name"];
			$this->view->category_id = $category[0]["category_id"];
			
			//Start------get name of category
			$nameCate = unserialize($category[0]['category_name']);
			if(isset($nameCate[$name['name']]) && isset($name)){
				$category_name = $nameCate[$name['name']];
			}else{
				$category_name = $nameCate['Default'];
			}
			$this->view->category_name = $category_name;
			//End--------get name of category
		}
		//End--------get catgory by id
		
		//Start------Khai báo các parameter
		$path = APPLICATION_PATH."/widgets/listshow/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Khai báo các parameter
		
		$user1 = new User();
		$user1->init();
		$user1->check_login();
		$hasmodify = $user1->modifiy_content($xmlconfig->name);
		if($hasmodify=='false')
		{
			$this->view->error = 6;
			return;
		}
		
		$session = new Zend_Session_Namespace('login.auth');
		if(!isset($session->id_user))
		{
			$this->view->error = 5;
		}
		else 
		{
			//Start------Get user
			$user = $tbuser->selectUser($session->id_user);
			//End--------Get user
		}
		
		if(isset($_SESSION["PostQA_Error"]))
		{
			$this->view->error = $_SESSION["PostQA_Error"];
			unset($_SESSION["PostQA_Error"]);
		}
		if(isset($_GET["action"])&&($_GET["action"]=="new")&&(!isset($this->view->error)))
		{
				if(!isset($_POST["QA_category"])||(!is_numeric($_POST["QA_category"]))||($_POST["QA_category"]<=0))
				{
					$this->view->error = 3;
				}
				
				$error_desc = array();
				
				//check multi-language
				if(!isset($_POST["QA_title"]['Default'])||(strlen($_POST["QA_title"]['Default'])<10))
				{
					$this->view->error = 2;
					$error_desc['Default'][0] = $language->get('error9');
				}
				if(!isset($_POST["QA_description"]['Default'])||(strlen($_POST["QA_description"]['Default'])<20))
				{
					$this->view->error = 4;
					$error_desc['Default'][1] = $language->get('error11');
				}
				
				foreach($languages as $lang){
					if(!isset($_POST["QA_title"][$lang['name']])||(strlen($_POST["QA_title"][$lang['name']])<10))
					{
						$this->view->error = 2;
						$error_desc[$lang['name']][0] = $language->get('error9');
					}
					if(!isset($_POST["QA_description"][$lang['name']])||(strlen($_POST["QA_description"][$lang['name']])<20))
					{
						$this->view->error = 4;
						$error_desc[$lang['name']][1] = $language->get('error11');
					}
				}
				
				$this->view->error_desc = $error_desc;
		}
		//End--------Check Input Parameter
		
		//Start------Get QA Category
		$CategoriesQA = $tbcategory->getLeafCategoriesbyType(3);
		//End--------Get QA Category
		
		if(isset($_GET["action"])&&($_GET["action"]=="new")&&(!isset($this->view->error)))
		{
			
			//get all languages
				$tbLanguage = new Language_Model_Language();
				$all_languages= $tbLanguage->getAllLanguages();
				//end get
				
				//insert title and decription into all languages
				$title_lang["Default"]="...";
				$description_lang["Default"]="-----------------------";
				$common_info_lang["Default"]="-----------------------";
				$short_des_lang["Default"]="-----------------------";
				if(isset($_POST["QA_title"]["Default"]))
					$title_lang["Default"]=$_POST["QA_title"]["Default"];
				if(isset($_POST["QA_description"]["Default"]))
				{
					$description_lang["Default"]=$_POST["QA_description"]["Default"];
					$short_des_lang["Default"]=substr($_POST["QA_description"]["Default"],0,50);
					$common_info_lang["Default"]=substr($_POST["QA_description"]["Default"],0,50);
				}
				
				foreach ($all_languages as $lang)
				{
					$title_lang[$lang["name"]]="...";
					$description_lang[$lang["name"]]="-----------------------";
					$short_des_lang[$lang["name"]]="-----------------------";
					$common_info_lang[$lang["name"]]="-----------------------";
					
					if(isset($_POST["QA_title"][$lang["name"]]))
						$title_lang[$lang["name"]]=$_POST["QA_title"][$lang["name"]];
					if(isset($_POST["QA_description"][$lang["name"]]))
					{
						$description_lang[$lang["name"]]=$_POST["QA_description"][$lang["name"]];
						$short_des_lang[$lang["name"]]=substr($_POST["QA_description"][$lang["name"]],0,50);
						$common_info_lang[$lang["name"]]=substr($_POST["QA_description"][$lang["name"]],0,50);
					}
					
				}
				//end insert
				
				$inserData = array(
									'category_id'	=>	$_POST["QA_category"],
									'user_id'		=>	$session->id_user,
									'title'			=>	serialize($title_lang),
									'description'	=>	serialize($description_lang),
									'type'			=>	3,
									'views'			=>	0,
									'created_date'	=>	$dateInt->get_time_now(),
									'update_date'	=>	$dateInt->get_time_now(),
									'deleted'		=>	0,
									'avatar'		=>	$user[0]["avatar"],
									'common_info'	=>	serialize($common_info_lang),
									'short_description'=> serialize($short_des_lang)
									);
				$result = $tbthings->insertThings($inserData);
			$this->view->error = $result;
		}
		if(isset($_POST["QA_title"]))
		{
			$this->view->title = $_POST["QA_title"];
		}
		else 
		{
			$this->view->title = "";
		}
		if(isset($_POST["QA_category"]))
		{
			$category_id = $_POST["QA_category"];
			$category = $tbcategory->getCategoriesbyId($category_id);
			$this->view->category_id = $category[0]["category_id"];
			
			//Start------get name of category
			$nameCate = unserialize($category[0]['category_name']);
			if(isset($nameCate[$name['name']]) && isset($name)){
				$category_name = $nameCate[$name['name']];
			}else{
				$category_name = $nameCate['Default'];
			}
			$this->view->category_name = $category_name;
			//End--------get name of category
		}
		else 
		{
			$this->view->category = 0;
		}
		if(isset($_POST["QA_description"]))
		{
			$this->view->description = $_POST["QA_description"];
		}
		else 
		{
			$this->view->description = "";
		}
		
		//Start------Truyền parameter qua View
		if(count($CategoriesQA)>0)
		{
			$this->view->CategoriesQA = $CategoriesQA;
		}
		else 
		{
			$this->view->CategoriesQA = array();
		}
		//End--------Truyền parameter qua View
	}
}